﻿package clips
{	
	import events.MoveEvent;
	import flash.display.MovieClip;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getQualifiedClassName;
	
	import flash.xml.XMLNode;
	import fl.motion.Color;
	//import com.actiplay.utils.Filtres;
	import flash.utils.describeType;

	public class Draggable extends MovieClip
	{
		static public var current:Draggable;
		static public var liste:Array = new Array();

		private var couleur : Color;
		private var _description : Object;
		public var lockCenter : Boolean = false;
		
		public function Draggable()
		{
			buttonMode = true;
			couleur = new Color();
			couleur.tintColor = 0x007CC1;
			couleur.tintMultiplier = 1;
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			addEventListener(MouseEvent.MOUSE_OVER, _survole );
			
			liste.push( this );
		}
		static public function getByName( nom:String ):Draggable
		{
			for each(var d:Draggable in liste ) if ( d.name == nom ) return d;
			return null;
		}
		public function get position():Point
		{
			return new Point( x, y );
		}		
		public function tint(c:int):void
		{
			couleur = new Color();
			couleur.tintColor = c;
			couleur.tintMultiplier = 1;
			transform.colorTransform = couleur;	
		}
		public function _survole(event:MouseEvent):void
		{
			//var description:XML = describeType(this);
            //trace(description);
            //trace(description..accessor.@name.toXMLString());
			
			removeEventListener(MouseEvent.MOUSE_OVER, _survole );
			addEventListener(MouseEvent.MOUSE_OUT, _sort );
			
			//Filtres.shadow(this, { color:0x000000, angleInDegrees:31, distance:3, blurX:4, blurY:4, quality:1, inner:false } );
			//Filtres.glow(this, { } );
		}
		private function _sort(event:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_OVER, _survole );
			removeEventListener(MouseEvent.MOUSE_OUT, _sort );
			
			//Filtres.remove(this);
		}			
		public function mouseDown(event:MouseEvent):void
		{
			current = this;
			startDrag( lockCenter );
			parent.setChildIndex( this, parent.numChildren - 1 );
			stage.addEventListener(MouseEvent.MOUSE_MOVE, bouge);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
			dispatchEvent( new MoveEvent( MoveEvent.STARTMOVING ) );
		}
		public function mouseReleased(event:MouseEvent):void
		{
			stopDrag();
			x = int( x );
			y = int( y );
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, bouge);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);
			dispatchEvent( new MoveEvent( MoveEvent.ENDMOVING ) );
		}
		public function bouge(event:MouseEvent):void
		{
			var mouse:Point = new Point( event.stageX, event.stageY );
			dispatchEvent( new MoveEvent( MoveEvent.MOVING, parent.globalToLocal( mouse ) ) );
		}		
		override public function toString():String
		{
			return "Draggable " + getQualifiedClassName( this );
		}
	}
}